package com.fa4j.system.app.repository.impl;

import com.fa4j.common.base.model.CommonConstants;
import com.fa4j.common.base.model.CommonState;
import com.fa4j.common.base.util.TreeUtil;
import com.fa4j.common.data.repository.impl.BaseRepositoryImpl;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.app.model.AppMenu;
import com.fa4j.system.app.repository.AppMenuRepository;
import com.fa4j.system.app.repository.mapper.AppMenuMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 系统应用菜单 持久化接口
 *
 * @since 2025-07-14 11:04:33
 */
@Repository
public class AppMenuRepositoryImpl extends BaseRepositoryImpl<AppMenuMapper, AppMenu> implements AppMenuRepository {

    @Override
    public List<AppMenu> queryTree(AppType appType, CommonState state, List<Integer> ids) {
        return TreeUtil.listToTree(
                super.lambdaQuery()
                        .select(p -> !Objects.equals("resourceIds", p.getColumn()))
                        .eq(AppMenu::getAppType, appType)
                        .eq(state != null, AppMenu::getState, state)
                        .in(ids != null, AppMenu::getId, ids)
                        .list(), CommonConstants.TREE_ROOT_ID
        );
    }

    @Override
    public Optional<AppMenu> findByAppTypeAndCode(AppType appType, String code) {
        return super.lambdaQuery()
                .eq(AppMenu::getAppType, appType)
                .eq(AppMenu::getCode, code)
                .oneOpt();
    }

    @Override
    public List<AppMenu> queryByParentId(Integer parentId) {
        return super.lambdaQuery()
                .eq(AppMenu::getParentId, parentId)
                .list();
    }
}
